<?php

require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/const.php' );
require_once( '../../libs/new_smt_variables.php' );

if( !in_array( "N", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

$staff_id = $_SESSION['ADMIN_LOGIN']['STAFF_ID'];

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	
	$mod = $_GET['mod'];
	
	if($mod=='new'){
		$order_id = $_GET['order_id'];
		$db = new Class_DB;
		$sql = "SELECT * FROM k_order_head WHERE order_id = '$order_id'";
		$result = $db->query($sql);
		$row = mysql_fetch_array($result);
		$row['o_date']=date('Y/m/d',strtotime($row['o_date']));
		$row['delivery_date']=date('Y/m/d',strtotime($row['delivery_date']));
		
		$smarty->assign('order', $row);
		$smarty->assign('flag', 1);
		$smarty->assign('tax', $tax);
	}
	
	else if($mod=='get'){
		$order_id = $_GET['order_id'];
		$db = new Class_DB;
		$sql = "SELECT * FROM k_order_head WHERE order_id = '$order_id'";
		$result = $db->query($sql);
		$row = mysql_fetch_array($result);
		$row['o_date']=date('Y/m/d',strtotime($row['o_date']));
		$row['delivery_date']=date('Y/m/d',strtotime($row['delivery_date']));
		$smarty->assign('order', $row);
		
		$sql = "SELECT * FROM k_order_detail as a WHERE a.order_id='$order_id'";
		$result = $db->query($sql);
		$row1 = array();
		while( $row = mysql_fetch_array($result,MYSQL_ASSOC) ){
			$row['money'] = number_format(intval($row['refer_price']*$row['quantity'])); 
			$row['refer_price'] =intval($row['refer_price']);
			$row1[] = $row;
		}
		$smarty->assign('detail', $row1);
		$smarty->assign('flag', 0);
		$smarty->assign('tax', $tax);
		$smarty->assign('num', count($row1)+1);
	}
	
	else if($mod=='delete'){
		$db = new Class_DB;
		$order_id = $_GET['order_id'];
		$db ->query("UPDATE k_order_head SET shipping='', discount=1,quote_special='',quote_remark='',tax=0 ,bookstatus=0 WHERE order_id = '$order_id'");
		$db ->query("DELETE FROM k_order_detail WHERE order_id = '$order_id'");
		$db ->close();
		$smarty->assign('order_id', $order_id);
		$smarty->assign('phase', 'completeupdate');
		
	}
	
}else{
	$mod = $_POST['mod'];
	if($mod == 'input'){
		$db = new Class_DB;
		$category = $_POST['category'];
		$pre = "P";
		if($category==3) $pre="M";
		if($category==4) $pre="A";
		$noSql = "SELECT * FROM k_order_head WHERE bookid like '".$pre."%' order by order_id desc limit 0,1";
		$result = $db->query($noSql);
		$row = mysql_fetch_array($result);
		$bookid = $pre;
		if(!empty($row['bookid'])){
			$obi = $row['bookid'];
			$bookid.=sprintf("%05.0f",intval(substr($obi, 1,strlen($obi)-1))+1);
		}else{
			$bookid.="00001";
		}
		
		$shipping = $_POST['shipping'];
		$discount = $_POST['discount'];
		$quote_special = $_POST['quote_special'];
		$quote_remark = $_POST['quote_remark'];
		$order_id		= $_POST['order_id'];
		$tax_table		= $_POST['tax'];
		$date = Date("Y/m/d");
		$head_sql = "UPDATE k_order_head SET shipping='$shipping', discount=$discount,quote_special='$quote_special',quote_remark='$quote_remark',tax=$tax_table,bookdate='$date',bookid='$bookid',bookstatus=1 WHERE order_id=$order_id";
		$db ->query($head_sql);
		
		//detail
		$shop_ids		= $_POST['shop_id'];
		$nums			= $_POST['num'];
		$brands			= $_POST['brand'];
		$refer_prices	= $_POST['refer_price'];
		$cost_prices	= $_POST['cost_price'];
		$shop_names		= $_POST['shop_name'];
		$sizes			= $_POST['size'];
		$s_units		= $_POST['s_unit'];
		$detail_sql_value		= "";
		for($i=0;$i<count($shop_ids);$i++){
			if($shop_ids[$i]>0){
				$detail_sql_value .="(null,'$order_id',$shop_ids[$i],$nums[$i],'$brands[$i]','$shop_names[$i]','$sizes[$i]',$refer_prices[$i],'$s_units[$i]',$cost_prices[$i]),";
			}
		}
		$detail_sql_value = substr($detail_sql_value, 0,strlen($detail_sql_value)-1);
		//echo $detail_sql_value;
		if(strlen($detail_sql_value)>0){
			$db ->query("insert into k_order_detail values $detail_sql_value");
		}
		$db ->close();
		$smarty->assign('order_id', $order_id);
		$smarty->assign('phase', 'complete');
		
	}
	
	else if($mod == 'update'){
		$db = new Class_DB;
		
		$category = $_POST['category'];
		$pre = "P";
		if($category==3) $pre="M";
		if($category==4) $pre="A";
		$noSql = "SELECT * FROM k_order_head WHERE bookid like '".$pre."%' order by order_id desc limit 0,1";
		$result = $db->query($noSql);
		$row = mysql_fetch_array($result);
		$bookid = $pre;
		if(!empty($row['bookid'])){
			$obi = $row['bookid'];
			$bookid.=sprintf("%05.0f",intval(substr($obi, 1,strlen($obi)-1))+1);
		}else{
			$bookid.="00001";
		}
		
		$shipping = $_POST['shipping'];
		$discount = $_POST['discount'];
		$quote_special = $_POST['quote_special'];
		$quote_remark = $_POST['quote_remark'];
		$order_id		= $_POST['order_id'];
		$tax_table		= $_POST['tax'];
		$date = Date("Y/m/d");
		
		$head_sql = "UPDATE k_order_head SET shipping='$shipping', discount=$discount,quote_special='$quote_special',quote_remark='$quote_remark',tax=$tax_table,bookdate='$date',bookid='$bookid' WHERE order_id=$order_id";
		$db ->query($head_sql);
		$db ->query("DELETE FROM k_order_detail WHERE order_id = $order_id");
		//detail
		$shop_ids		= $_POST['shop_id'];
		$nums			= $_POST['num'];
		$brands			= $_POST['brand'];
		$refer_prices	= $_POST['refer_price'];
		$cost_prices	= $_POST['cost_price'];
		$shop_names		= $_POST['shop_name'];
		$sizes			= $_POST['size'];
		$s_units		= $_POST['s_unit'];
		$detail_sql_value		= "";
		for($i=0;$i<count($shop_ids);$i++){
			if($shop_ids[$i]>0){
				$detail_sql_value .="(null,'$order_id',$shop_ids[$i],$nums[$i],'$brands[$i]','$shop_names[$i]','$sizes[$i]',$refer_prices[$i],'$s_units[$i]',$cost_prices[$i]),";
			}
		}
		$detail_sql_value = substr($detail_sql_value, 0,strlen($detail_sql_value)-1);
		//echo $detail_sql_value;
		if(strlen($detail_sql_value)>0){
			$db ->query("insert into k_order_detail values $detail_sql_value");
		}
		$db ->close();
		$smarty->assign('order_id', $order_id);
		$smarty->assign('phase', 'completeupdate');
	
	}
	
	
}


	
$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );
$smarty->assign('menu_html', $menu_html);
$smarty->assign('tax', $tax);
$smarty->display('web-admin/order_make_price.html');


?>